﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Dycox.Workflows.Manipulation
{
    internal class MoveForwardState
    {
        public MoveForwardState(WorkflowForwardingContext context)
        {
            _exit = new ExitStageTransitionContext(context);
            _noMoreStep = new NoMoreStepTransitionContext(context);
        }

        public WorkflowForwardingContext ExecutionContext
        {
            get { return (WorkflowForwardingContext)_exit.ExecutionContext; }
        }

        private ExitStageTransitionContext _exit;

        public ExitStageTransitionContext Exit
        {
            get { return _exit; }
        }

        private NoMoreStepTransitionContext _noMoreStep;

        public NoMoreStepTransitionContext NoMoreStep
        {
            get { return _noMoreStep; }
        }

        private List<UpcomingState> _upcomings;

        public List<UpcomingState> Upcomings
        {
            get
            {
                if (_upcomings == null)
                    _upcomings = new List<UpcomingState>();
                return _upcomings;
            }
        }

        public bool HasUpcomings
        {
            get
            {
                return _upcomings != null && _upcomings.Count > 0;
            }
        }

        private StageOperationContract _userState;

        public StageOperationContract UserState
        {
            get
            {
                if (_userState == null)
                {
                    _userState = ExecutionContext.CreateContract();
                }
                return _userState;
            }
            set { _userState = value; }
        }

        public IReflectionRoot AppObject
        {
            get { return ExecutionContext.AppObject; }
        }

        public ConditionTestArguments CreateTestArgs()
        {
            return new ConditionTestArguments(ExecutionContext.Step, UserState, ExecutionContext.Stage, AppObject);
        }


    }

    internal class UpcomingState
    {
        public UpcomingState(MoveForwardState owner, WFStep newStep)
        {
            StepContext upcomingStep = new StepContext(newStep, owner.ExecutionContext.Workflow);
            _normal = new NormalStageTransitionContext(owner.ExecutionContext, upcomingStep);
        }

        private NormalStageTransitionContext _normal;

        public NormalStageTransitionContext Normal
        {
            get { return _normal; }
        }

    }
}
